package com.jicheng.service.impl;


import com.jicheng.Utils.DESEncryptUtil;
import com.jicheng.Utils.PicUtil;
import com.jicheng.dto.account.AccountParams;
import com.jicheng.dto.user.SuperviseUserParams;
import com.jicheng.dto.user.UserDataParams;
import com.jicheng.dto.user.UserDto;
import com.jicheng.dto.user.UserPagingParams;
import com.jicheng.entity.SuperviseUser;
import com.jicheng.ext.dto.PagingResult;
import com.jicheng.ext.util.BeanConvertor;
import com.jicheng.ext.util.UUIDUtils;
import com.jicheng.mapper.SuperviseUserMapper;
import com.jicheng.service.IUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private SuperviseUserMapper superviseUserMapper;

    @Transactional
    @Override
    public void create(SuperviseUserParams params) {
        notNull("params",params);
        SuperviseUser userEntity = BeanConvertor.copy(params, SuperviseUser.class);
        superviseUserMapper.insertSelective(userEntity);
    }

    @Transactional
    @Override
    public void update(SuperviseUserParams params) {
        notNull("params",params);
        SuperviseUser userEntity = BeanConvertor.copy(params, SuperviseUser.class);
        superviseUserMapper.updateByPrimaryKeySelective(userEntity);
    }

    @Override
    public List<UserDto> getList(UserPagingParams params) {
        notNull("params",params);

        List<UserDto> list = superviseUserMapper.getList(params);
        return list;
    }

    @Override
    public UserDto getById(String id) {
        notNull("id",id);
        SuperviseUser superviseUser = superviseUserMapper.selectByPrimaryKey(id);

        if(superviseUser.getHeader()!=null && superviseUser.getHeader()!=""){
            File file = new File(superviseUser.getHeader());
            if(file.exists()){
                try {
                    String base64 = DESEncryptUtil.decryption("H:/key.k", superviseUser.getHeader().replace("\\", "/"), "decrypt");
                    superviseUser.setHeader("data:image/jpg;base64,"+base64);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }else {
                superviseUser.setHeader(null);
            }
        }
        return superviseUser == null ? null : BeanConvertor.copy(superviseUser, UserDto.class);
    }

    @Transactional
    @Override
    public void syncData(UserDataParams params) {

    }

    @Transactional
    @Override
    public void saveAccount(AccountParams params) {
        notNull("params",params);


    }

    @Override
    public int exist(String appId) {
        return superviseUserMapper.exist(appId);
    }

}
